var utils = require("../../utils/utils");
var ocrGml = require("../../utils/ocrGml");
var readArticle = require("../appCommon/readArticle");

var ad = {
  isOver: function () {
    return utils.existsTextStartsWith("领取成功");
  },
  watchAd() {
    sleep(5 * 1000);
    var adType = this.getAdType();
    this.watchAdType(adType);
  },
  getAdType() {
    for (var index = 0; index < 5; index++) {
      var content = "直播间观看时长";
      if (utils.existsTextStartsWith(content)) {
        return 4;
      }
      var content = "秒后得";
      if (utils.existsTextEndsWith(content)) {
        return 3;
      }
      var content = "跳过";
      if (utils.existsTextEndsWith(content)) {
        return 2;
      }
      var content = "后可领奖励";
      if (utils.existsTextEndsWith(content)) {
        return 1;
      }
      sleep(2 * 1000);
    }
  },
  watchAdType(type) {
    var map = {
      1: "后可领奖励",
      2: "跳过",
      3: "秒后得",
      4: "直播间观看时长",
    };
    console.log("watchAdType:", type, "/", map[type]);
    if (type == 4) {
      this.watchAdType4(utils.getRandom(50, 50 + 3));
    }
    if (type == 3) {
      this.watchAdType3();
    }
    if (type == 2) {
      this.watchAdType2();
    }
    if (type == 1) {
      this.watchAdType1(utils.getRandom(50, 50 + 3));
    }
  },
  watchAdType4(span) {
    var startTime = Date.now();
    var count = 0;
    while (true) {
      var hasWatch = (Date.now() - startTime) / 1000;
      if (hasWatch > span) {
        console.log("最长广告时间已达，强制结束");
        break;
      }
      sleep(2 * 1000);
      count++;
      if (count % 5 == 0) {
        console.log("广告已看：" + hasWatch, "/", span);
      }
      if (hasWatch >= 10) {
        if (utils.clickTextStartsWith("取消")) {
          sleep(utils.getRandom(1.5, 2.5) * 1000);
        }
      }
      if (this.isOver()) {
        var interval = utils.getRandom(2, 4);
        console.log("广告已结束,再过-", interval, "-后关闭");
        sleep(interval * 1000);
        break;
      }
    }
    this.checkBack();
    this.back();
  },
  checkBack() {
    for (var index = 0; index < 5; index++) {
      sleep(utils.getRandom(1.5, 2.5, 10000));
      var content = "退出";
      if (utils.clickTextEndsWith(content)) {
        return true;
      }
      var content = "后可领奖励";
      if (utils.existsTextEndsWith(content)) {
        return true;
      }
      var content = "广告";
      if (utils.existsTextStartsWith(content)) {
        return true;
      }
      var content = "领取成功";
      if (utils.existsTextStartsWith(content)) {
        return true;
      }
      back();
    }
  },
  watchAdType3() {
    var span = utils.getRandom(32, 35);
    readArticle(span, 3);
    this.back();
  },
  watchAdType2() {
    var span = utils.getRandom(65, 70);
    var startTime = Date.now();
    var count = 0;
    var checkActivity = false;

    while (true) {
      var hasWatch = (Date.now() - startTime) / 1000;
      if (hasWatch > span) {
        console.log("最长广告时间已达，强制结束");
        break;
      }
      sleep(5 * 1000);
      count++;
      if (count % 5 == 0) {
        console.log("广告已看：" + hasWatch, "/", span);
      }
      var content = "关闭";
      if (utils.clickTextEndsWith(content)) {
        this.back(true);
        return;
      }
      if (!checkActivity) {
        if (hasWatch > 15) {
          var content = ["后过期", "后结束"];
          var ocrItem = ocrGml.captureExistOne(content);
          if (ocrItem) {
            span = span + 30;
            checkActivity = true;
          }
        }
      }
    }
    var content = "跳过";
    if (utils.clickTextEndsWith(content)) {
      this.back(true);
      return;
    }
    var content = "关闭";
    if (utils.clickTextEndsWith(content)) {
      this.back(true);
      return;
    }
  },
  watchAdType1(span) {
    var startTime = Date.now();
    var count = 0;
    while (true) {
      var hasWatch = (Date.now() - startTime) / 1000;
      if (hasWatch > span) {
        console.log("最长广告时间已达，强制结束");
        break;
      }
      sleep(2 * 1000);
      count++;
      if (count % 5 == 0) {
        console.log("广告已看：" + hasWatch, "/", span);
      }
      if (hasWatch >= 20) {
        if (!utils.existsTextStartsWith("广告")) {
          back();
          sleep(utils.getRandom(1.5, 2.5) * 1000);
        }
      }
      if (this.isOver()) {
        var interval = utils.getRandom(2, 4);
        console.log("广告已结束,再过-", interval, "-后关闭");
        sleep(interval * 1000);
        if (!utils.existsTextStartsWith("广告")) {
          back();
          sleep(utils.getRandom(1.5, 2.5) * 1000);
        }
        break;
      }
    }
    this.back();
  },
  back(notNeedBack) {
    if (!notNeedBack) {
      back();
    }
    sleep(utils.getRandom(2, 3, 1000));
    var content = "领取奖励";
    if (utils.clickTextStartsWith(content)) {
      sleep(utils.getRandom(2.5, 3, 1000));
      this.watchAd();
      return;
    }
    // var content = "坚持退出";
    // if (utils.clickTextStartsWith(content)) {
    //   sleep(utils.getRandom(2.5, 3, 1000));
    // }
    var content = "再看一个获取";
    if (utils.clickTextStartsWith(content)) {
      sleep(utils.getRandom(2.5, 3, 1000));
      this.watchAd();
      return;
    }
    // var content = "坚持退出";
    // if (utils.clickTextStartsWith(content)) {
    //   sleep(utils.getRandom(2.5, 3, 1000));
    // }
    var content = "再看一个继续";
    if (utils.clickTextStartsWith(content)) {
      sleep(utils.getRandom(2.5, 3, 1000));
      this.watchAd();
      return;
    }
    // var content = "直接退出";
    // if (utils.clickTextStartsWith(content)) {
    //   sleep(utils.getRandom(2.5, 3, 1000));
    // }
  },
};
module.exports = ad;
